package com.woniu.controller;

import cn.hutool.core.lang.UUID;
import com.woniu.config.MinIOConfig;
import com.woniu.util.MinIODir;
import com.woniu.util.MinIOUtils;
import com.woniu.util.ResponseData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

@RestController
@RequestMapping("/upload")
@CrossOrigin(origins = "*")
@Slf4j
public class UploadFileController {

 @Resource
 private MinIOConfig minIOConfig;

    @PostMapping("/imgFile")
    public ResponseData<?> imgFile(@RequestBody MultipartFile file) throws Exception {
        String fileName = MinIODir.DIR_SYSTEM + file.getName() + UUID.fastUUID();
        MinIOUtils.uploadFile(minIOConfig.getBucketName(), fileName, file.getInputStream());
        //http://127.0.0.1:9000/personimage/600.webp
        //http://127.0.0.1:9000/personimage/system/file0fb99f28-6d46-4dd0-9bc1-189cb5502daf
        String newFileName = minIOConfig.getFileHost() + "/" + minIOConfig.getBucketName() + file.getName();

        return ResponseData.ok(newFileName);
    }
}
